Event.observe ( window, 'load', bootstrap );

function bootstrap () {
  TableKit.options.editAjaxURI = '/rest/specification';
  
  table_kit_select_input_as_boolean ( $w( 'authorization_owner authorization_writer authorization_follower' ) );
  
  $( 'specification_id'     ).on ( 'change',  specification_changed        ); /* mouseup */
  $( 'user'                 ).on ( 'change',  user_changed                 );
  
  $( 'neo_specification_id' ).on ( 'change',  neo_specification_id_changed ); /* keyup */
  $( 'neo_repository_id'    ).on ( 'change',  neo_repository_id_changed    );
  
  var re = '^(';
  $$( 'select#specification_id option' ).each ( function ( node ) { re = re + node.label + '|' } );
  re = re + '-)$';
  names_re = new RegExp ( re );
  
  reset_users_table ();
}

function neo_specification_id_changed () {
  var specification = $( 'neo_specification_id' ).value;
  
  var name_re = new RegExp ( '^[A-Z][A-Z0-9-]+$' );
  
  if ( name_re.test ( specification ) && ! names_re.test ( specification ) ) {
    enable  ( 'neo_repository_id' );
  }
  else {
    disable ( 'neo_repository_id' );
  }
}

function neo_repository_id_changed () {
  if ( $( 'neo_repository_id' ).value != '' ) {
    enable  ( 'create' );
  }
  else {
    disable ( 'create' );
  }
}

function user_changed () {
  var user          = $( 'user'             ).value;
  var specification = $( 'specification_id' ).value;
  
  if ( user != '' && specification != '' ) {
    new Ajax.Request ( '/rest/user/' + user + '/specification/' + specification + '?authorization=writer', {
      method         : 'put',
      requestHeaders : { Accept : 'application/json' },
      onComplete     : function () {
        specification_changed ();
        
        $( 'user' ).setValue ( '' );
      }
    } );
  }
}

function specification_changed () {
  var specification = $( 'specification_id' ).value;
  
  if ( specification != '' ) {
    new Ajax.Request ( '/rest/specification/' + specification, {
      method         : 'get',
      requestHeaders : { Accept : 'application/json' },
      onComplete     : function ( response ) {
        $( 'users_table' ).replace ( users_authorizations_table ( response.responseJSON, 'specification', specification ) );
        
        TableKit.Sortable.init ( 'users_table' );
        TableKit.reload ();
        
        enable ( 'user' );
        $( 'user' ).style.display = '';
        
        $( 'specification_repository_id' ).replace ( "<td id=\"specification_repository_id\" title=\"" + response.responseJSON.repository_url + "\">" + response.responseJSON.repository_id + "</td>" );
        disable ( 'specification_id_filter' );
      },
      onFailure      : function ( response ) {
        reset_users_table ();
      }
    } );
  }
  else {
    reset_users_table ();
  }
}

function reset_users_table () {
  $( 'users_table' ).replace ( '<div id="users_table"></div>' );
  
  disable ( 'user' );
  $( 'user' ).style.display = 'none';
  
  $( 'specification_repository_id' ).replace ( "<td id=\"specification_repository_id\"></td>" );
  enable ( 'specification_id_filter' );
}
